package com.sys.server.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sys.server.common.constants.enums.ResultSetEnum;
import com.sys.server.common.model.R;
import com.sys.server.modules.controller.vo.PageVo;
import com.sys.server.modules.entity.ProjectEntity;
import com.sys.server.modules.mapper.ProjectMapper;
import com.sys.server.modules.service.WorkService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class WorkServiceImpl implements WorkService {

    private static final Logger LOG = LoggerFactory.getLogger(WorkServiceImpl.class);

    @Resource
    private ProjectMapper projectMapper;

    @Override
    public R getWorkList(Long pagenum, Long pagesize) {
        IPage<ProjectEntity> page = new Page<>(pagenum, pagesize);
        QueryWrapper<ProjectEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("ispublic", "是");
        try {
            projectMapper.selectPage(page, wrapper);
        } catch (Exception e){
            LOG.error("分页获取要展示的学生作品错误" + e.getMessage());
            return R.ok(ResultSetEnum.FAIL);
        }
        PageVo result = new PageVo();
        result.setCurrent(pagenum);
        result.setSize(pagesize);
        result.setList(page.getRecords());
        result.setTotal(page.getTotal());
        return R.ok(ResultSetEnum.SUCCESS, result);
    }
}
